<html>
<head>
<title>锁具移交</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<link rel="shortcut icon" href="resources/images/lock.png" type="image/x-icon">
<link rel="stylesheet" href="resources/css/stylemain.css">
<link rel="stylesheet" href="resources/layui/css/layui.css">

<link rel="stylesheet" href="resources/layui/css/treeTable.css">
<link rel="stylesheet" href="resources/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<script src="resources/layui/layui.all.js"></script>
<script type="text/javascript" src="resources/js/jquery.js"></script>
<script type="text/javascript" src="resources/js/base64.js"></script>
<style type="text/css">
.layui-form-checkbox[lay-skin=primary] {
	height: 18px !important;
}

.layui-transfer-header .layui-form-checkbox[lay-skin=primary] {
    margin-top: 10px !important;
}
</style>
</head>
<body>
<form class="layui-form" action="" style="margin-top: 30px;">
	<div class="layui-form-item">
		<div class="layui-inline">
		    <label class="layui-form-label">所属机构</label>
		    <div class="layui-input-inline">
		      <input type="button" id="f_orgname" name="orgname" onclick="dialog_orgselect()" class="layui-input" style="text-align: left;">
		    </div>
	    </div>
		<div class="layui-inline">
		    <label class="layui-form-label">使用人</label>
		    <div class="layui-input-inline">
		      	<select name="owner" id="owner" lay-filter="fowner" lay-search="owner">
		          <option value="">直接选择或搜索选择</option>
		        </select>
		    </div>
	    </div>
	    <div class="layui-inline">
	    	<label class="layui-form-label">移交人</label>
		    <div class="layui-input-inline">
		      <select name="newowner" id="newowner" lay-verify="required" lay-search="newowner">
		          <option value="">直接选择或搜索选择</option>
		        </select>
		    </div>
	    </div>
	    <div class="layui-inline">
	    	<button type="button" class="layui-btn" id="btn_save">立即提交</button>		    
	    </div>
	  </div>	  
</form>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>选择需移交的锁具</legend>
</fieldset>
<input id="org_id" type="text" name="orgid" style="display:none"></input>
<div id="locktrans" class="lock-transfer" style="margin-left:15px"></div>
<script type="text/javascript">
var transfer = layui.transfer;
var layer = layui.layer;
var form = layui.form;

layui.use([], function(){
	debugger;	
	loadinitdata();
	$("#btn_save").click(function() {
		if($('#newowner').val() == ""){
			layer.msg('需选择移交人!');
			return;
		}
		transfersave("","");
		
	});
	
	form.on('select(fowner)', function(data){
		  if(data.value != ""){
			  getUserNameList();
			  getUserLocks();
			  form.render();
		  }
	});  
	
	$("#org_id").on("input",function(e){
	    //获取input输入的值
	    var datastring = '{"orgid":"' + e.delegateTarget.value +'"}';
		debugger;
		$.ajax({
			type:"post",
			contentType: "application/json;charset=UTF-8",
			url: "getUserNamelist.do?",
			async:false,
			data:datastring,
	     	dataType:"json",
	        success: function(msg) {    //数据提交成功时返回数据
	        	//debugger;
	        	if(msg.status == "1"){
	        		$("#owner option[value !='']").remove();
	        		for(var i=0,l=msg.datas.length;i<l;i++){
	        			$("#owner").append("<option value='"+msg.datas[i].username+"'>"+msg.datas[i].realname+"</option>");
	        		 }
	        		form.render('select');
	        	}else{
	        		layer.msg(msg.desc);
	        	}						
	        },
	        error : function(e){
	        	if(e.status == 200){
	        		parent.location.href = "login";
	        	}else{
	        		alert(e.status);
	                alert(e.responseText);
	        	}
	        }
	
		});		
	});
	form.render();
});

function loadinitdata(){
	var datastring = '{"rname":"智能锁管理/锁具管理/锁具移交","ckname":"","ckpassword":""}';
	$.ajax({
		type:"post",
		contentType: "application/json;charset=UTF-8",
		url: "lockstransfer_init.do?",
		async:false,
		data:datastring,
     	dataType:"json",
        success: function(msg) {    //数据提交成功时返回数据
        	//debugger;
        	if(msg.status == "1"){
        		transrender([]);
        		//layer.msg(msg.desc, {icon: 1});
        	}else{
        		$('#f_orgname').attr("disabled","disabled");
        		layer.msg(msg.desc, {icon: 5});
        	}						
        },
        error : function(e){
        	if(e.status == 200){
        		parent.location.href = "login";
        	}else{
        		alert(e.status);
                alert(e.responseText);
        	}
        }

	});		
}

function transfersave(ckname,ckpassword){
	debugger;
	if($("#owner").val() == "" || $('#newowner').val() == ""){
		layer.msg("请指定使用人和移交人后提交!");
		return;
	}
	var translocks = transfer.getData('lockoftransfer'); 
	if(translocks.length == 0){
		layer.msg("请指定需移交的锁具!");
		return;
	}
	var datastring = '{"owner":"' + $("#owner").val() + '","newowner":"' + $('#newowner').val() + '","translocks":' + JSON.stringify(translocks) + ',"rname":"智能锁管理/锁具管理/锁具移交","ckname":"'+ckname+'","ckpassword":"'+ckpassword+'"}';
	$.ajax({
		type:"post",
		contentType: "application/json;charset=UTF-8",
		url: "lockstransfer.do?",
		async:false,
		data:datastring,
     	dataType:"json",
        success: function(msg) {    //数据提交成功时返回数据
        	//debugger;
        	if(msg.status == "1"){
        		getUserLocks();
        		layer.msg(msg.desc, {icon: 1});
        	}else if(msg.status == "1211"){

        		$('#ckname').val('');
				$('#ckpassword').val('');
				ckdialgo_auth();
        		
        	}else{
        		layer.msg(msg.desc, {icon: 5});
        	}						
        },
        error : function(e){
        	if(e.status == 200){
        		parent.location.href = "login";
        	}else{
        		alert(e.status);
                alert(e.responseText);
        	}
        }

	});		
}

function getUserNameList(){
	var datastring = '{"orgid":"' + $('#org_id').val() +'"}';
	$.ajax({
		type:"post",
		contentType: "application/json;charset=UTF-8",
		url: "getUserNamelist.do?",
		async:false,
		data:datastring,
	 	dataType:"json",
	    success: function(msg) {    //数据提交成功时返回数据
	    	//debugger;
	    	if(msg.status == "1"){
	    		$("#newowner option[value !='']").remove();
	    		var tmp = $('#owner').val();
	    		for(var i=0,l=msg.datas.length;i<l;i++){
	    			if(tmp == msg.datas[i].username){
	    				continue;
	    			}
	    			$("#newowner").append("<option value="+msg.datas[i].username+">" + msg.datas[i].username + "-" + msg.datas[i].realname + "</option>");
	    		}
	    		 
			}else{
				layer.msg(msg.desc);
			}	
	    	
	    },        
	    error : function(e){
	    	if(e.status == 200){
        		parent.location.href = "login";
        	}else{
        		alert(e.status);
                alert(e.responseText);
        	}
	    }
	
	});
	
}

function getUserLocks(){
	var datastring = '{"username":"' + $("#owner").val() + '"}';
	$.ajax({
		type:"post",
		contentType: "application/json;charset=UTF-8",
		url: "getUserLocks.do?",
		async:false,
		data:datastring,
	 	dataType:"json",
	    success: function(msg) {    //数据提交成功时返回数据
	    	//debugger;
	    	if(msg.status == "1"){
	    		transrender(msg.datalist);
			}else{
				layer.msg(msg.desc);
			}	
	    	
	    },        
	    error : function(e){
	    	if(e.status == 200){
        		parent.location.href = "login";
        	}else{
        		alert(e.status);
                alert(e.responseText);
        	}
	    }
	
	});
}

function transrender(datas){
	transfer.render({
	    elem: '#locktrans'
	    ,title: ['拥有的锁具', '移交的锁具']
		,id: 'lockoftransfer'
		,parseData: function(res){
	      return {
	        "value": res.locksn //数据值
	        ,"title": res.locksn + "-" + (res.area==""?res.boxid:res.area) //数据标题
	      }
	    }
	    ,data: datas
	    ,width: 330 //定义宽度
	    ,height: 'full-20'
	  })
}


$('#ckpassword').on('keydown', function (event) {
    if (event.keyCode == 13) {
    	userckopt();
		layer.closeAll();
    }
});


function ckdialgo_auth(){
	layer.open({
		  type: 1 //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层） 
		  ,title: "复核" //标题
		  ,skin:"layui-layer-lan"   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
		  ,area: ['350px', '260px'] //宽度、高度
		  ,shade: 0.3
		  ,offset: 'auto'
		  ,anim:5 //弹出动画
		  ,shadeClose: false //弹框出现后，允许点击其它地方关闭
		  ,content: $("#dlgck")     //内容
		  ,btn: ['确定', '取消'] //只是为了演示
		  ,success:function(layero){
			     //var mask = $(".layui-layer-shade");
			     //mask.appendTo(layero.parent());
			     $('#ckname').focus();
			     //其中：layero是弹层的DOM对象
			}
		 ,yes: function(index, layero){
		    //按钮【按钮一】的回调
		    	userckopt();
	    		$("#dlgck").css('display','none');
			 	layer.close(index);
		    	
			 	 
		  }
		  ,btn2: function(index, layero){
		    	//按钮【按钮二】的回调
		    
		    	$("#dlgck").css('display','none'); 
		    	
		  }
		  ,cancel: function(){ 
			    //右上角关闭回调
			    $("#dlgck").css('display','none'); 
			    
		 }
	});
}

function userckopt(){
	//debugger;
	var ckname = $('#ckname').val();
	if(ckname == ''){
		layer.msg('请填写用户账号!');
		return;
	}
	var ckpassword = $('#ckpassword').val();
	transfer(ckname,$.base64.encode(ckpassword));
}

function userckopt_cancel(){
	$('#ckname').val('');
	$('#ckpassword').val('');	
}

function dialog_orgselect(){
	layer.open({
		  type: 2 //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层） 
		  ,title: "选择机构" //标题
		  ,skin:"layui-layer-lan"   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
		  ,area: ['350px', '400px'] //宽度、高度
		  ,shade: 0.3
		  ,offset: 'auto'
		  ,anim:5 //弹出动画
		  ,shadeClose: false //弹框出现后，允许点击其它地方关闭
		  ,content: 'getorgtree.html'     //内容
		  ,btn: ['取消'] //只是为了演示		
		  ,success:function (layero,index) {
			  debugger;
              var iframe = window['layui-layer-iframe' + index];              
              iframe.child("locktransfer_org","porg");
      	  }
		  ,yes: function(index, layero){
		    //按钮【按钮一】的回调
			 	 //$("#orgtree").css('display','none'); 
			 	layer.close(index);
		  }
		  ,cancel: function(index,layero){ 
			    //右上角关闭回调
			    //$("#orgtree").css('display','none'); 
		 }
       });
}
</script>
</body>
</html>
